package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.mapper.LitemallCategoryDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallCategoryService {
    @Resource
    private LitemallCategoryDao categoryDao;
    private LitemallCategory.Column[] CHANNEL = {LitemallCategory.Column.id, LitemallCategory.Column.name, LitemallCategory.Column.iconUrl};

    public List<LitemallCategory> queryL1WithoutRecommend(int offset, int limit) {
    	LambdaQuery<LitemallCategory> query = categoryDao.createLambdaQuery()
    			.andNotEq(LitemallCategory::getName, "推荐")
    			.andEq(LitemallCategory::getLevel, "L1")
    			.andEq(LitemallCategory::getDeleted, false);
    	query.limit(offset, limit);
    	List<LitemallCategory> list = query.select();
    	return list;
		/*
		 * LitemallCategoryExample example = new LitemallCategoryExample();
		 * example.or().andLevelEqualTo("L1").andNameNotEqualTo("推荐").andDeletedEqualTo(
		 * false); PageHelper.startPage(offset, limit); return
		 * categoryMapper.selectByExample(example);
		 */
    }

    public List<LitemallCategory> queryL1(int offset, int limit) {
    	
    	LambdaQuery<LitemallCategory> query = categoryDao.createLambdaQuery()
    			.andEq(LitemallCategory::getLevel, "L1")
    			.andEq(LitemallCategory::getDeleted, false);
    	query.limit(offset, limit);
    	List<LitemallCategory> list = query.select();
    	return list;
    	
		/*
		 * LitemallCategoryExample example = new LitemallCategoryExample();
		 * example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
		 * PageHelper.startPage(offset, limit); return
		 * categoryMapper.selectByExample(example);
		 */
    }

    public List<LitemallCategory> queryL1() {
    	
    	LambdaQuery<LitemallCategory> query = categoryDao.createLambdaQuery()
    			.andEq(LitemallCategory::getLevel, "L1")
    			.andEq(LitemallCategory::getDeleted, false);
    	List<LitemallCategory> list = query.select();
    	return list;
    	
		/*
		 * LitemallCategoryExample example = new LitemallCategoryExample();
		 * example.or().andLevelEqualTo("L1").andDeletedEqualTo(false); return
		 * categoryMapper.selectByExample(example);
		 */
    }

    public List<LitemallCategory> queryByPid(Integer pid) {
    	LambdaQuery<LitemallCategory> query = categoryDao.createLambdaQuery()
    			.andEq(LitemallCategory::getPid, pid)
    			.andEq(LitemallCategory::getDeleted, false);
    	List<LitemallCategory> list = query.select();
    	return list;
		/*
		 * LitemallCategoryExample example = new LitemallCategoryExample();
		 * example.or().andPidEqualTo(pid).andDeletedEqualTo(false); return
		 * categoryMapper.selectByExample(example);
		 */
    }

    public List<LitemallCategory> queryL2ByIds(List<Integer> ids) {
    	
    	LambdaQuery<LitemallCategory> query = categoryDao.createLambdaQuery()
    			.andIn(LitemallCategory::getPid, ids)
    			.andEq(LitemallCategory::getLevel, "L2")
    			.andEq(LitemallCategory::getDeleted, false);
    	List<LitemallCategory> list = query.select();
    	return list;
		/*
		 * LitemallCategoryExample example = new LitemallCategoryExample();
		 * example.or().andIdIn(ids).andLevelEqualTo("L2").andDeletedEqualTo(false);
		 * return categoryMapper.selectByExample(example);
		 */
    }

    public LitemallCategory findById(Integer id) {
    	return categoryDao.single(id);
        //return categoryMapper.selectByPrimaryKey(id);
    }

    public List<LitemallCategory> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallCategory> query = categoryDao.createLambdaQuery();
    	
    	if (!StringUtils.isEmpty(id)) {
    		query.andEq( LitemallCategory::getId , Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
        	query.andLike(LitemallCategory::getName, "%" + name + "%");
            //criteria.andNameLike("%" + name + "%");
        }
        query.andEq(LitemallCategory::getDeleted, false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        query.limit(page, size);
    	List<LitemallCategory> list = query.select();
    	return list;
		/*
		 * 
		 * LitemallCategoryExample example = new LitemallCategoryExample();
		 * LitemallCategoryExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(id)) { criteria.andIdEqualTo(Integer.valueOf(id)); }
		 * if (!StringUtils.isEmpty(name)) { criteria.andNameLike("%" + name + "%"); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, size); return
		 * categoryMapper.selectByExample(example);
		 */
    }

    public int updateById(LitemallCategory category) {
        category.setUpdateTime(LocalDateTime.now());
        return categoryDao.updateTemplateById(category);
        //return categoryMapper.updateByPrimaryKeySelective(category);
    }

    public void deleteById(Integer id) {
    	
    	LitemallCategory category = new LitemallCategory();
    	category.setId(id);
    	category.setDeleted(true);
    	categoryDao.updateTemplateById(category);
        //categoryMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallCategory category) {
        category.setAddTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryDao.insertTemplate(category,true);
        //categoryMapper.insertSelective(category);
    }

    public List<LitemallCategory> queryChannel() {
    	LitemallCategory category = new LitemallCategory();
    	category.setLevel("L1");
        category.setDeleted(false);
        return categoryDao.template(category);
		/*
		 * LitemallCategoryExample example = new LitemallCategoryExample();
		 * example.or().andLevelEqualTo("L1").andDeletedEqualTo(false); return
		 * categoryMapper.selectByExampleSelective(example, CHANNEL);
		 */
    }
}
